package com.sunpy.niohttp.etc;

import com.sunpy.niohttp.model.SunpyRequest;
import com.sunpy.niohttp.model.SunpyResponse;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Worker {


    /**
     * 包装response响应对象
     * @param sunpyRequest
     * @param sunpyResponse
     */
    public void packResponse(SunpyRequest sunpyRequest, SunpyResponse sunpyResponse) {
        /**
         * "application/json;charset=UTF-8"
         */
        sunpyResponse.setVersion(sunpyRequest.getVersion());
        Map<String, String> headers = sunpyResponse.getHeaders();

        if (CollectionUtils.isEmpty(headers)) {
            headers = new HashMap<>();
        }

        headers.put("Server", "nio-http/1.1");

        if (headers.get("Content-Type") == null) {
            headers.put("Content-Type", "text/plain;charset=UTF-8");
        }

        Calendar calendar = Calendar.getInstance();
        headers.put("Date", calendar.getTime().toString());
        sunpyResponse.setHeaders(headers);
    }

}
